package com.example.demo.service.impl;

import com.example.demo.entity.Role;
import com.example.demo.service.BosUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class BosUserServiceImpl implements BosUserService {
    @Autowired
    private static RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        BosUserServiceImpl.redisTemplate = redisTemplate;
    }

    /**
     * 1.测试redis的使用
     */
    @Override
    public void testRedis() {
        Role role = new Role("TEST_Role","测试","管理员");
        //1.将对象保存到redis中
        redisTemplate.opsForValue().set("userModel",role);
        //2.从redis中取出这个对象。
        Role role1 = (Role) redisTemplate.opsForValue().get("userModel");
        System.out.println("用户数据：---"+role1);
    }

    @Override
    public void testRedis1() {
        redisTemplate.delete("userModel");//删除一个key
        Boolean hasKey = redisTemplate.hasKey("userModel");//删除一个key
        System.out.println(hasKey);
        DataType userModel = redisTemplate.type("userModel");//判断key所对应值的类型
        System.out.println(userModel);
        Set<String> keys = redisTemplate.keys("*");//获取redis中所有值
        keys.forEach(key -> System.out.println("key = " +key ));
    }
}